﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Sol394
{

    public string DecodeString(string s)
    {

        //遇到括号入栈，遇到括号出栈,处理括号里面还有括号
        //注意，数字可能有两位，所有遇到一个数字，后面不一定是[
        Stack<string> strS = new Stack<string>();
        Stack<int> numS = new Stack<int>();
        string curStr = "";

        int len=s.Length;
        int k = 0;//记住是数字的十位还是个位
        for (int i = 0; i < len; i++)
        {
            char ch = s[i];
            if(char.IsDigit(ch)){//如果是数字，累加

                k = k * 10 + (ch-'0');
            }
            else{

                if (ch == '[')
                {
                    numS.Push(k);
                    strS.Push(curStr);//存嵌套括号
                    curStr = "";
                    k = 0;

                }
                else if(ch == ']')
                {
                    string temp=strS.Pop();
                    int repeatTime=numS.Pop();

                    for (int j = 0; j < repeatTime; j++)
                    {
                        temp += curStr; // 重复当前字符串
                    }
                    //更新字符串
                    curStr = temp;

                }
                else
                {
                    //遇到字母
                    curStr += ch;
                }


            }
        
        }


        return curStr;
    }



}
    class Program394
{


    static void Main394(string[] args)
    {
        Sol394 sol394 = new Sol394();
        string s = "3[a]2[bc]";
        string res = sol394.DecodeString(s);

        Console.WriteLine(res);

    }
}
